package nostalgia.collection;

import nostalgia.lastfm.model.Track;

import java.util.Date;

public class DirectoryCollectionItem {
    private String artist;
    private String title;
    private long duration;
    private String file;
    private long lastUpdate;

    public boolean isMedia() {
        return artist != null;
    }

    public String getArtist() {
        return artist;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public long getDuration() {
        return duration;
    }

    public void setDuration(long duration) {
        this.duration = duration;
    }

    public String getFile() {
        return file;
    }

    public void setFile(String file) {
        this.file = file;
    }

    public long getLastUpdate() {
        return lastUpdate;
    }

    public void setLastUpdate(long lastUpdate) {
        this.lastUpdate = lastUpdate;
    }

    @Override
    public int hashCode() {
        return getArtist().toLowerCase().hashCode() + getTitle().toLowerCase().hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if(obj instanceof DirectoryCollectionItem) {
            return getArtist().equalsIgnoreCase(((DirectoryCollectionItem)obj).getArtist()) &&
                    getTitle().equalsIgnoreCase(((DirectoryCollectionItem)obj).getTitle());
        }
        else if(obj instanceof Track) {
            return getArtist().equalsIgnoreCase(((Track)obj).getArtist().getValue()) &&
                    getTitle().equalsIgnoreCase(((Track)obj).getName());
        }
        else {
            return false;
        }
    }

    @Override
    public String toString() {
        StringBuffer fctval = new StringBuffer();
        fctval.append(getFile());
        fctval.append("\t");
        fctval.append(getLastUpdate());
        if(isMedia()) {
            fctval.append("\t");
            fctval.append(getArtist());
            fctval.append("\t");
            fctval.append(getTitle());
            fctval.append("\t");
            fctval.append(getDuration());
        }
        return fctval.toString();
    }

    public static DirectoryCollectionItem parse(String item) {
        String parts[] = item.split("\t");
        DirectoryCollectionItem fctval = new DirectoryCollectionItem();
        int i = 0;
        fctval.setFile(parts[i++]);
        fctval.setLastUpdate(Long.parseLong(parts[i++]));
        if(i < parts.length) {
            fctval.setArtist(parts[i++]);
            fctval.setTitle(parts[i++]);
            fctval.setDuration(Long.parseLong(parts[i++]));
        }
        return fctval;
    }
}
